//
//  OrderDetailAllowanceCell.m
//  danpin
//
//  Created by chuangjia on 30/10/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "OrderDetailAllowanceCell.h"

@interface OrderDetailAllowanceCell ()
{
    
}
@property(nonatomic,strong)UILabel *sectionLabel;
@property(nonatomic,strong)UIButton *helpbtn;
@property(nonatomic,strong)UIView *lineview;
//
@property(nonatomic, strong) TTTAttributedLabel* title;


@end
@implementation OrderDetailAllowanceCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        
        _sectionLabel =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2,20)];
        _sectionLabel.backgroundColor = [UIColor clearColor];
        _sectionLabel.textColor = CONTEXT_COLOR;
        _sectionLabel.textAlignment = NSTextAlignmentLeft;
        _sectionLabel.font = FONT(12);
        [self.contentView addSubview:_sectionLabel];
        
        //告诉用户总价的计算方式，各个细项的收费原因
        _helpbtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _helpbtn.backgroundColor = [UIColor clearColor];
        _helpbtn.tag=1;
        _helpbtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _helpbtn.layer.borderWidth = 1;
        _helpbtn.titleLabel.font = FONT(12);
        _helpbtn.userInteractionEnabled = YES;
        _helpbtn.hidden = YES;
        _helpbtn.frame =CGRectMake(PHOTO_FRAME_WIDTH,(40-20)/2, PHOTO_FRAME_WIDTH*6, 20);
        [_helpbtn addTarget:self action:@selector(clickHelpEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_helpbtn];
      
        
        _lineview =[[UIView alloc] initWithFrame:CGRectZero];
        _lineview.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineview];
        
        //
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,40)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkTextColor];//UIColorFromRGB(0x808080);
        _title.textAlignment = NSTextAlignmentLeft;
        _title.lineBreakMode = NSLineBreakByWordWrapping|NSLineBreakByTruncatingTail;
        _title.numberOfLines=0;
        _title.font = FONT(12);
        [self.contentView addSubview:_title];
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock =nil;
    self.data=nil;
    self.indexPath=nil;
    self.other=nil;
    self.key=nil;
    
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key =key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    
    self.data =model;
    //
    NSDictionary *order_tariff =[dict dictionaryObjectForKey:@"order_tariff"];
    if (order_tariff==nil || ![order_tariff isKindOfClass:[NSDictionary class]]) {
        return;
    }
    _sectionLabel.text =@"补费明细";
    _sectionLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, 0,DEVICE_WIDTH, 40);
    int status =[[order_tariff numberStringForKey:@"status"] intValue];
    _helpbtn.frame =CGRectMake(DEVICE_WIDTH-PHOTO_FRAME_WIDTH*7,(40-20)/2, PHOTO_FRAME_WIDTH*6, 20);
  
    if (status==0) {
        _helpbtn.hidden = NO;
         [_helpbtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_helpbtn setTitle:@"去支付" forState:UIControlStateNormal];
        _helpbtn.enabled = YES;
    }else{
        _helpbtn.hidden=NO;
        [_helpbtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_helpbtn setTitle:@"已支付" forState:UIControlStateNormal];
        _helpbtn.enabled=NO;
    }
   
    _lineview.frame =CGRectMake(PHOTO_FRAME_WIDTH, 39.5,DEVICE_WIDTH, 0.5);
    //
    
    float fheight = 0;
    NSString *title =[order_tariff stringForKey:@"message" withDefaultValue:@""];
    if (title.length>0) {
        [self.title setText:title afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName
                                      value:FONT(12)
                                      range:NSMakeRange(0,[attributedString length])];
             //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
             
             CGFloat lineSpace = 1.0f;
             CTParagraphStyleSetting lineSpaceStyle;
             lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
             lineSpaceStyle.valueSize = sizeof(lineSpace);
             lineSpaceStyle.value =&lineSpace;
             
             //设置文本段间距
             CGFloat paragraphSpacing = 0.0f;
             CTParagraphStyleSetting paragraphSpaceStyle;
             paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
             paragraphSpaceStyle.valueSize = sizeof(CGFloat);
             paragraphSpaceStyle.value = &paragraphSpacing;
             
             //创建设置数组
             CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
             CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
             
             //给文本添加设置
             [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
             
             return attributedString;
             
         }];
        
        fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*2) str:self.title.attributedText];
        
        self.title.frame = CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*5,DEVICE_WIDTH- PHOTO_FRAME_WIDTH*2, fheight);
    }
    
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)clickHelpEvent:(id)sender
{
    NSDictionary *order_tariff =[self.data dictionaryObjectForKey:@"order_tariff"];
    if (order_tariff==nil || ![order_tariff isKindOfClass:[NSDictionary class]]) {
        return;
    }
    int status =[[order_tariff numberStringForKey:@"status"] intValue];
    if (status==0) {
        if (self.actionBlock) {
            self.actionBlock(BEventType_tariff,nil,self.data,nil,self.indexPath);
        }
    }
}

/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    CGFloat hight =PHOTO_FRAME_WIDTH*6;
    NSDictionary *dict =(NSDictionary*)model;
    NSDictionary *order_tariff =[dict dictionaryObjectForKey:@"order_tariff"];
    if (order_tariff && [order_tariff isKindOfClass:[NSDictionary class]]) {
        float fheight = 0;
        NSString *title =[order_tariff stringForKey:@"message" withDefaultValue:@""];
        if (title.length>0) {
            NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:title];
            [attributedString addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(0,[attributedString length])];
            
            CGFloat lineSpace = 1.0f;
            CTParagraphStyleSetting lineSpaceStyle;
            lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
            lineSpaceStyle.valueSize = sizeof(lineSpace);
            lineSpaceStyle.value =&lineSpace;
            //设置文本段间距
            CGFloat paragraphSpacing = 0.0f;
            CTParagraphStyleSetting paragraphSpaceStyle;
            paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
            paragraphSpaceStyle.valueSize = sizeof(CGFloat);
            paragraphSpaceStyle.value = &paragraphSpacing;
            //创建设置数组
            CTParagraphStyleSetting settings[ ] ={lineSpaceStyle};
            CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,1);
            
            //给文本添加设置
            [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
            fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- PHOTO_FRAME_WIDTH*2) str:attributedString];
        }
        
        hight= hight +fheight;
    }
  

    return hight;
}

@end
